package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: hea7en
 * @Date: 2024/5/19 3:35 PM
 * @Description: 套餐管理控制层
 */
@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐管理相关接口")
public class SetmealController {

    @Autowired
    SetmealService setmealService;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CachePut(cacheNames = "setmealCache", key = "#setmealDTO.id")
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐：{}", setmealDTO);
        setmealService.saveSetmealWithDish(setmealDTO);

        return Result.success();
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("分页查询：{}", setmealPageQueryDTO);
        PageResult result = setmealService.pageQuery(setmealPageQueryDTO);

        return Result.success(result);
    }

    /**
     * 启用停售套餐
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用停售套餐")
    public Result startOrStop(@PathVariable Integer status, Long id){
        log.info("停售/启售套餐：{}", id);
        setmealService.startOrStop(status, id);

        return Result.success();
    }

    /**
     * 通过id查询套餐详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("通过id查询套餐")
    @Cacheable(cacheNames = "setmealCache", key = "#id")
    public Result<SetmealVO> getById(@PathVariable Long id){
        log.info("通过id查询套餐：{}", id);

        return Result.success(setmealService.getById(id));
    }

    @PutMapping
    @ApiOperation("修改套餐")
    @CachePut(cacheNames = "setmealCache", key = "#setmealDTO.id")
    public Result editSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐：{}", setmealDTO);
        setmealService.updateSetmealWithDish(setmealDTO);

        return Result.success();
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除套餐")
    @CacheEvict(cacheNames = "setmealCache", key = "#ids")
    public Result deleteBatch(@RequestParam List<Long> ids){
        log.info("批量删除套餐：{}", ids);
        setmealService.deleteBatch(ids);

        return Result.success();
    }
}
